package com.webchat.user.service.relation;


import com.webchat.common.bean.APIPageResponseBean;
import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.enums.AccountRelationTypeEnum;
import com.webchat.common.enums.RoleCodeEnum;
import com.webchat.domain.vo.request.CreateGroupRequestVO;
import com.webchat.domain.vo.response.AccountRelationListVO;
import com.webchat.domain.vo.response.AccountRelationResponseVO;
import com.webchat.domain.vo.response.UserBaseResponseInfoVO;
import com.webchat.domain.vo.response.WaitConfirmUserResponseVO;
import com.webchat.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountRelationService {

    @Autowired
    private UserService userService;

    @Autowired
    private User2UserAccountRelationService user2UserAccountRelationService;
    @Autowired
    private User2GroupAccountRelationService user2GroupAccountRelationService;

    /**
     * 订阅/关注
     *
     * @param sourceAccount
     * @param targetAccount
     * @return
     */
    public Boolean subscribe(String sourceAccount, String targetAccount) {
        UserBaseResponseInfoVO targetAccountInfo = userService.getUserBaseInfoByUserId(targetAccount);
        Integer roleCode = targetAccountInfo.getRoleCode();
        return AccountRelationFactory.getServiceByCode(roleCode).subscribe(sourceAccount, targetAccount);
    }

    /**
     * 取消订阅/关注
     *
     * @param sourceAccount
     * @param targetAccount
     * @return
     */
    public Boolean unsubscribe(String sourceAccount, String targetAccount) {
        UserBaseResponseInfoVO targetAccountInfo = userService.getUserBaseInfoByUserId(targetAccount);
        Integer roleCode = targetAccountInfo.getRoleCode();
        return AccountRelationFactory.getServiceByCode(roleCode).unsubscribe(sourceAccount, targetAccount);
    }

    /**
     * 确认订阅/关注
     *
     * @param confirmAccount
     * @param relationId
     * @return
     */
    public Boolean confirmSubscribe(String confirmAccount, Long relationId) {

        return user2UserAccountRelationService.confirmSubscribe(confirmAccount, relationId);
    }

    /**
     * 拒绝订阅/关注
     *
     * @param rejectAccount
     * @param relationId
     * @return
     */
    public Boolean rejectSubscribe(String rejectAccount, Long relationId) {

        return user2UserAccountRelationService.rejectSubscribe(rejectAccount, relationId);
    }

    public APIPageResponseBean<UserBaseResponseInfoVO> pageRelations(String account, Integer type,
                                                              Integer pageNo, Integer pageSize) {

        return AccountRelationFactory.getServiceByType(type).listRelations(account, pageNo, pageSize);
    }


    /**
     * 查询账号好友列表
     *
     * @param account
     * @return
     */
    public AccountRelationResponseVO listRelations(String account) {
        int pageNo = 1;
        int pageSize = Integer.MAX_VALUE;
        return AccountRelationResponseVO.builder()
                .users(this.getAccountRelationVO(AccountRelationTypeEnum.USER_USER.getType(), account, pageNo, pageSize))
                .groups(this.getAccountRelationVO(AccountRelationTypeEnum.USER_GROUP.getType(), account, pageNo, pageSize))
                .officials(this.getAccountRelationVO(AccountRelationTypeEnum.USER_OFFICIAL.getType(), account, pageNo, pageSize))
                .robots(this.getAccountRelationVO(AccountRelationTypeEnum.USER_ROBOT.getType(), account, pageNo, pageSize))
                .build();
    }

    /**
     * 查询账号的好友关系
     *
     * @param account
     * @return
     */
    public List<WaitConfirmUserResponseVO> listWaitConformUsers(String account) {
        /**
         * 因为好友关系审核只存在与 user -> user 关系中，这里我们直接调用具体实现类获取数据即可
         */
        return user2UserAccountRelationService.listWaitConformUsers(account);
    }

    /**
     * 基于工厂模式，获取对应关系实现类，完成好友关系列表查询
     * 走redis，性能完全ok
     *
     * @param relationType
     * @param account
     * @param pageNo
     * @param pageSize
     * @return
     */
    private AccountRelationListVO getAccountRelationVO(Integer relationType, String account, int pageNo, int pageSize) {
        APIPageResponseBean<UserBaseResponseInfoVO> apiPageResponseBean =
                AccountRelationFactory.getServiceByType(relationType).listRelations(account, pageNo, pageSize);
        return AccountRelationListVO.builder()
                .count(apiPageResponseBean.getTotal())
                .accounts(apiPageResponseBean.getData())
                .build();
    }

    /**
     * 创建群聊
     *
     * @param createGroupRequest
     * @return
     */
    public String createChatGroup(CreateGroupRequestVO createGroupRequest) {

        return user2GroupAccountRelationService.createChatGroup(createGroupRequest);
    }

}
